package sjtu.ist.warehouse4ws.tool;
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException; 
public class MyUtils {
	 /** 
     * 上传文件 
     *  
     * @param savePath 
     *            文件的保存路径 
     * @param uploadFile 
     *            被上传的文件 
     * @return newFileName 
     */  
    public static String upload(String uploadFileName, String savePath, File uploadFile) {  
        String newFileName = uploadFileName;
                       
        try {  
        	File file=new File(savePath + newFileName);
        	if(!file.getParentFile().exists()){
        		file.getParentFile().mkdirs();
        	}
            FileOutputStream fos = new FileOutputStream(savePath + newFileName);  
            FileInputStream fis = new FileInputStream(uploadFile);  
            byte[] buffer = new byte[1024];  
            int len = 0;  
            while ((len = fis.read(buffer)) > 0) {  
                fos.write(buffer, 0, len);  
            }  
            
            
            fis.close();
            fos.close();
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return newFileName;  
    }  
  
    public static String getUUIDName(String fileName, String dir) {  
        String[] split = fileName.split("\\.");  
        String extendFile = "." + split[split.length - 1].toLowerCase();  
        return java.util.UUID.randomUUID().toString() + extendFile;  
    }  
  
    /** 
     * 根据路径创建一系列的目录 
     *  
     * @param path 
     */  
    public static boolean mkDirectory(String path) {  
        File file = null;  
        try {  
            file = new File(path);  
            if (!file.exists()) { 
            
                return file.mkdirs();  
            }  
        } catch (RuntimeException e) {  
            e.printStackTrace();  
        } finally {  
            file = null;  
        }  
        return false;  
    }  
    public static boolean moveFile(String oldFile,String newFile){
    	try{
    		File   srcFile   =   new   File(oldFile);//存在的源文件 
        	File   dtnFile   =   new   File(newFile);   //制定目标文件路径以及文件名 
        	srcFile.renameTo(dtnFile);
    	}catch(Exception e){
    		return false;
    	}
    	
		return true;    	
    }
    public static boolean deleteFile(String path){
    	try{
    		File   srcFile   =   new   File(path);//存在的源文件 
        	srcFile.delete();
    	}catch(Exception e){
    		return false;
    	}
    	
		return true;    
    }
}
